home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OTPAPSampleServer / PAPServerUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.6 KB  |  155 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3.     file: PAPServerUtilities.c
  4.     
  5.     by:        Rich Kubota
  6.             Developer Technical Support
  7.             
  8. */
  9.  
  10. #include <Errors.h>
  11. #include <Files.h>
  12. #include <Traps.h>
  13. #include <Devices.h>
  14. #include "PAPServerUtilities.h"
  15.  
  16. /* the following code is from IM VI 3-8 which demonstrates how to check for the 
  17.  * availability of traps
  18.  */
  19.  
  20. short NumToolboxTraps(void)
  21. {
  22.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  23.         return(0x200);
  24.     else
  25.         return(0x400);
  26.  
  27. } /* NumToolboxTraps */
  28.  
  29. TrapType GetTrapType(short theTrap)
  30. {
  31.     short    trapMask = 0x0800;
  32.     
  33.     if (theTrap & trapMask)
  34.         return(ToolTrap);
  35.     else
  36.         return(OSTrap);
  37.  
  38. } /* GetTrapType */
  39.  
  40. Boolean TrapAvailable(short theTrap)
  41. {
  42.     TrapType    theType;
  43.     
  44.     theType = GetTrapType(theTrap);
  45.     if (theType == ToolTrap) {
  46.         theTrap &= 0x07FF;
  47.         if (theTrap >= NumToolboxTraps())
  48.             theTrap = _Unimplemented;
  49.     }
  50.     return(NGetTrapAddress(theTrap, theType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  51. }
  52.  
  53. /***************************************************************************
  54. The following function was taken from the MoreFiles.c DTS Sample code.
  55. **/
  56.  
  57. pascal    OSErr    HCreateMinimum(short vRefNum,
  58.                                long dirID,
  59.                                ConstStr255Param fileName)
  60. {
  61.     HParamBlockRec pb;
  62.  
  63.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  64.     pb.fileParam.ioVRefNum = vRefNum;
  65.     pb.ioParam.ioVersNum = 0;
  66.     pb.fileParam.ioDirID = dirID;
  67.     return ( PBHCreateSync(&pb) );
  68. }
  69.  
  70.  
  71. /* 
  72.  * OpenTempFile opens a temporary file on the root volume of the boot drive
  73.  * returns the file ref num to write to, or -1 if a problem occurred
  74.  *
  75.  * This code is not meant to demonstrate how one goes about creating a temporary
  76.  * file, it's more a matter of expediency for me.
  77.  */
  78. OSErr    OpenTempFile(short    *fRefNum)
  79. {
  80.     HParamBlockRec    pb;
  81.     short            index = 0;
  82.     OSErr            err;
  83.     
  84.     Str255            fname = "\pSavedPAPFile";
  85.                         //       123456789012        temp file name is 12 characters long
  86.     
  87.         // initialize err as dupFNErr since that and noErr are the only errors
  88.         // which we can deal with here
  89.     err = dupFNErr;
  90.     while (err == dupFNErr)
  91.     {
  92.         if (index < 10)
  93.         {
  94.             fname[0] = 14;    // specify that the file name is 14 character pascal string
  95.             fname[14] = '0' + index;
  96.         }
  97.         else if (index < 100)    // temp files 'SavedPAPFile 0' - 'SavedPAPFile 9' already exist
  98.         {
  99.             fname[0] = 15;
  100.             fname[14] = '0' + (index / 10);        // set the tens placeholder
  101.             fname[15] = '0' + (index % 10);        // set the units placeholder
  102.         }
  103.         else // this part of the code will likely not be tested, but it's implemented just in case.
  104.         {
  105.             fname[0] = 16;
  106.             fname[14] = '0' + (index / 100);        // set the hundreds placeholder
  107.             fname[15] = '0' + ((index % 100) / 10);    // set the tens placeholder
  108.             fname[16] = '0' + (index % 10);            // set the units placeholder
  109.         }
  110.         
  111.         index++;      // increment index for the next go around;
  112.         
  113.         err = HCreateMinimum(kBootVolVRefNum, kRootFolderDirID, fname);
  114.     }
  115.     
  116.         // was the file created successfully
  117.         
  118.     if (err == noErr)
  119.     {
  120.         pb.fileParam.ioNamePtr = (StringPtr) fname;
  121.         pb.ioParam.ioPermssn = fsWrPerm;
  122.         pb.ioParam.ioMisc = 0;
  123.         pb.fileParam.ioDirID = kRootFolderDirID;
  124.         pb.fileParam.ioVRefNum = kBootVolVRefNum;
  125.         err = PBHOpenSync(&pb);
  126.     }
  127.     
  128.     if (err != noErr)
  129.         *fRefNum = 0;
  130.     else
  131.         *fRefNum = pb.ioParam.ioRefNum;
  132.         
  133.     return err;
  134. }
  135.  
  136.  
  137. OSErr WriteDataToTempFile(short fRefNum, UInt8 *buffer, UInt32 len)
  138. {
  139.     HParamBlockRec    pb;
  140.  
  141.     pb.ioParam.ioRefNum = fRefNum;
  142.     pb.ioParam.ioBuffer = (Ptr)buffer;
  143.     pb.ioParam.ioReqCount = len;
  144.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;  /* fsAtMarK + noCacheBit */
  145.     pb.ioParam.ioPosOffset = 0;
  146.     return (PBWriteSync((ParmBlkPtr)&pb));    
  147. }
  148.  
  149. OSErr CloseTempFile(short fRefNum)
  150. {
  151.     HParamBlockRec    pb;
  152.  
  153.     pb.ioParam.ioRefNum = fRefNum;
  154.     return (PBCloseSync((ParmBlkPtr)&pb));    
  155. }